﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SharepointLogReader
{
    public partial class Main : Form
    {
        #region Events
        public Main()
        {
            InitializeComponent();
        }
        private void Main_Load(object sender, EventArgs e)
        {
            SetLastLogDir();
            SetLastSearchTexts();
        }
        private void bOpenFile_Click(object sender, EventArgs e)
        {
            ChooseLogFile();
        }
        private void bSearch_Click(object sender, EventArgs e)
        {
            Search();
        }
        private void bStop_Click(object sender, EventArgs e)
        {
            StopWork();
        }
        private void tLogFolder_TextChanged(object sender, EventArgs e)
        {
            SaveLastLogDir();
        }
        #endregion

        #region Properties
        private string SearchText
        {
            get
            {
                return cbSearhText.Text;
            }
        }
        private string DirLogPath
        {
            get
            {
                return tLogFolder.Text;
            }
            set
            {
                tLogFolder.Text = value;
            }
        }
        private string SearchResultText
        {
            get
            {
                return rtbSearchResult.Text;
            }
            set
            {
                rtbSearchResult.Text = value;
            }
        }
        private string LogText
        {
            get
            {
                return rtbLog.Text;
            }
            set
            {
                rtbLog.Text = value;
            }
        }
        public int SearchResultsLinesCount
        {
            get
            {
                return rtbSearchResult.Lines.Length;
            }
        }
        private bool IsWorkStop { get; set; }
        #endregion

        #region MainFunctions
        private void SetLastLogDir()
        {
            DirLogPath = LogDirManager.GetLastLogDir();
        }
        private void SaveLastLogDir()
        {
            LogDirManager.SaveLastLogDir(DirLogPath);
        }
        private void SetLastSearchTexts()
        {
            List<string> allSearchTexts = GetSearchTextsLog();
            
            cbSearhText.Items.Clear();
            cbSearhText.Items.AddRange(allSearchTexts.ToArray());

            if (allSearchTexts.Count > 0)
            {
                cbSearhText.Text = allSearchTexts[0];
            }
        }
        private void ChooseLogFile()
        {
            if (Directory.Exists(DirLogPath))
            {
                fLogDir.SelectedPath = DirLogPath;
            }

            fLogDir.ShowDialog();

            DirLogPath = fLogDir.SelectedPath;
        }
        
        private void Search()
        {
            try
            {
                LockForm(true);
                AddSearchTextToLog(SearchText);

                SearchResultText = String.Empty;
                LogText = String.Empty;
                Application.DoEvents();

                if (string.IsNullOrEmpty(SearchText))
                {
                    MessageBox.Show(Texts.WRITE_SEARCH_TEXT_MESSAGE);
                    return;
                }

                if (Directory.Exists(DirLogPath))
                {
                    LogReader logReader = new LogReader();
                    logReader.Search(DirLogPath, SearchText, AddSearchText, AddLog, IsStopWork);

                    if (string.IsNullOrEmpty(SearchResultText))
                    {
                        SearchResultText = Texts.TEXT_NOT_FOUND_MESSAGE;
                    }
                }
                else
                {
                    SearchResultText = String.Empty;
                    MessageBox.Show(Texts.CHOOSE_FOLDER_MESSAGE);
                }
            }
            finally
            {
                LockForm(false);
            }
        }
        private void StopWork()
        {
            IsWorkStop = true;
        }
        #endregion

        #region AddFunctions
        private void AddLog(string message)
        {
            LogText += string.Format("{0} - {1}\r\n", DateTime.Now, message);
            ScrollToEnd(rtbLog);
            Application.DoEvents();
        }
        private void AddSearchText(string searchText)
        {
            SearchResultText = searchText + SearchResultText;
            ScrollToEnd(rtbSearchResult);
            Application.DoEvents();

            if (SearchResultsLinesCount >= Settings.Current.MaxSearchLines)
            {
                StopWork();
                AddLog(string.Format("Found more than {0} lines.", Settings.Current.MaxSearchLines));
            }
        }
        private void ScrollToEnd(RichTextBox rtb)
        {
            if (rtb.Text.Length > 0)
            {
                rtb.Select(rtb.Text.Length - 1, 0);
                rtb.ScrollToCaret();
            }
        }
        private bool IsStopWork()
        {
            return IsWorkStop;
        }

        private void AddSearchTextToLog(string searchText)
        {
            SearchTextsLog.AddSearchTextToLog(searchText);
            SetLastSearchTexts();
        }
        private List<string> GetSearchTextsLog()
        {
            return SearchTextsLog.GetSearchTextsLog();
        }

        private void LockForm(bool isLock)
        {
            bOpenFile.Enabled = !isLock;
            bSearch.Enabled = !isLock;
            bStop.Visible = isLock;
            IsWorkStop = !isLock;
            cbSearhText.Enabled = !isLock;
            tLogFolder.Enabled = !isLock;
            Application.DoEvents();
        }
        #endregion
    }
}
